﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public class ValueAndReference
    {
        public void RunTest()
        {
            int i = 5;

            testValueByValue(i);
            MessageBox.Show("testValueByValue(int i): " + i.ToString());

            testValueByReference(ref i);
            MessageBox.Show("testValueByReference(ref int i): " + i.ToString());

            testValueOut(out i);
            MessageBox.Show("testValueOut(out i): " + i.ToString());

            MyClass obj = new MyClass();

            testReferenceByReference(obj);
            MessageBox.Show("testReferenceByReference(obj): " + obj.I.ToString());

            testReferenceByReferenceToReference(ref obj);
            MessageBox.Show("testReferenceByReferenceToReference(ref obj): " + obj.I.ToString());

            obj = null;
            testReferenceOut(out obj);
            MessageBox.Show("testReferenceOut(out obj): " + obj.I.ToString());
        }

        private void testValueByValue(int i)
        {
            i++;
        }

        private void testValueByReference(ref int i)
        {
            i++;
        }

        private void testValueOut(out int i)
        {
            i = 4;
        }

        private void testReferenceByReference(MyClass obj)
        {
            obj.I++;
        }

        private void testReferenceByReferenceToReference(ref MyClass obj)
        {
            obj = new MyClass();
            obj.I++;
        }

        private void testReferenceOut(out MyClass obj)
        {
            obj = new MyClass();
            obj.I--;
        }
    }

    public class MyClass
    {
        public int I = 5;
    }
}
